package com.cn.ccp.webservice.impl;

import com.cn.ccp.bean.UserBean;


/**
 * 用户session
 * @author renyongcheng
 *
 */
final public class UserSession {

		private int userId;//用户id
		private long creationTime = 0L;//session 创建时间
	    /**
	     * The last accessed time for this Session.
	     */
		private volatile long lastAccessedTime = creationTime;
		private boolean isValid = true;//该会话是否处于激活状态
		private SessionManager manager;
		private UserBean u ;
		
		UserSession(SessionManager manager,UserBean u){
			this.manager = manager;
			this.u = u;
			creationTime = System.currentTimeMillis();
			lastAccessedTime = creationTime;
		}
		
		/**
		 * 会话过期处理
		 */
		void expire(){
			isValid = false;
			manager.remove(this);
			u = null;
		}
		
		/**
		 * 返回年级id
		 * @return
		 */
		public String gradeId(){
			return u.getGrade();
		}
		
		public int getUserId() {
			return u.getId();
		}
		
		public String getGrade(){
			return u.getGrade();
		}
		
		public String getSpecialtyId(){
			return u.getSpecialtyId();
		}
		
		public String getFacultyId(){
			return u.getFacultyId();
		}
		
		public String getUserName(){
			return u.getUserName();
		}
		
		
		public long getCreationTime() {
			return creationTime;
		}
		
		public long getLastAccessedTime() {
			return lastAccessedTime;
		}
		public void setLastAccessedTime(long lastAccessedTime) {
			this.lastAccessedTime = lastAccessedTime;
		}
		public boolean isValid() {
			return isValid;
		}
		public void setUnValid() {
			isValid = false;
			manager.remove(this);
		}

		@Override
		public String toString() {
			return "UserSession [userId=" + userId + ", creationTime="
					+ creationTime + ", lastAccessedTime=" + lastAccessedTime
					+ "]";
		}
		
		/**
		 * 修改用户图片
		 * @param newPath
		 * @return
		 */
		public String changeHeadImage(String newPath){
			String oldPath = u.getImage();
			u.setImage(newPath);
			return oldPath;
		}
		
		
		
}
